package com.ruoyi.api.task;

import com.ruoyi.api.queue.RequestOrderQueue;
import com.ruoyi.api.vo.AsyncVo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.shop.domain.ShopUserOrder;
import com.ruoyi.shop.service.IShopUserOrderService;
import com.ruoyi.shop.vo.UserOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Author Alan Zhang
 * @Date 2021/10/12 10:56
 */

@Component
public class OrderTask extends Thread {
    @Autowired
    private IShopUserOrderService shopUserOrderService;
    @Autowired
    private RequestOrderQueue queue;

    private boolean running = true;

    @Override
    public void run() {
        while (running) {
            try {
                AsyncVo<UserOrderVo, Object> vo = queue.getOrderQueue().take();
                System.out.println("[ OrderTask ]开始处理订单");

                UserOrderVo params = vo.getParams();

                ShopUserOrder order = shopUserOrderService.genBuyOrder(params);

                Thread.sleep(3000);


                vo.getResult().setResult( AjaxResult.success(order));

                System.out.println("[ OrderTask ]订单处理完成");
            } catch (InterruptedException e) {
                e.printStackTrace();
                running = false;
            }

        }
    }

    public void setRunning(boolean running) {
        this.running = running;
    }
}
